package com.simulation.web.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simulation.web.domain.TechTeamMembers;
import com.simulation.web.dto.TechTeamMembersDTO;
import com.simulation.web.mapper.*;
import com.simulation.web.service.TeamMemberService;
import com.simulation.web.utils.RelatedTypeEnum;
import com.simulation.web.vo.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author 唐雄杰
 * @Date 2024-08-25 22:19
 **/
@Service
@AllArgsConstructor
@Slf4j
public class TeamMemberServiceImpl implements TeamMemberService {
    private final TeamMemberMapper teamMemberMapper;

    private final TaskMapper taskMapper;

    private final PostMapper postMapper;

    private final CollectionMapper collectionMapper;

    private final TaskSubmissionsMapper taskSubmissionsMapper;

    private final MinioTemplate minioTemplate;

    @Override
    public void batchAddTeamMembers(List<TechTeamMembers> members) {
        List<TechTeamMembers> mems = new ArrayList<>();
        members.stream().forEach(member -> {
            member.setJoinedTime(LocalDateTime.now());
            TechTeamMembersVO techTeamMembersVO = teamMemberMapper.selectByTeamIdAndUserId(member.getTeamId(),member.getUserId());
            if (techTeamMembersVO ==null || Objects.isNull(techTeamMembersVO.getId())) {
                mems.add(member);
            }
        });
        if (CollectionUtil.isEmpty(mems)) {
            log.info("添加人员数据为空");
            return;
        }
        teamMemberMapper.batchInsertMembers(mems);
    }

    @Override
    public PageInfo<TechTeamMembersVO> getTeamMembersPaged(TechTeamMembersDTO techTeamMembersDTO) {
        PageHelper.startPage(techTeamMembersDTO.getPageNum(), techTeamMembersDTO.getPageSize());
        List<TechTeamMembersVO> membersList = teamMemberMapper.getTeamMembersPaged(techTeamMembersDTO);
        return new PageInfo<>(membersList);
    }

    @Override
    public void batchRemoveTeamMembers(Long teamId, List<Long> memberIds) {
        // 查询当前团队有哪些任务
        List<TechTaskVO> techTaskVOList = taskMapper.getTasksByTeamId(teamId);
        // 查询团队成员对应的userId
        List<TechTeamMembersVO> membersVOList = teamMemberMapper.getByIdList(memberIds);
        if (CollectionUtil.isNotEmpty(techTaskVOList) && CollectionUtil.isNotEmpty(membersVOList)) {
            List<Long> taskIdList = techTaskVOList.stream().map(TechTaskVO::getId).collect(Collectors.toList());
            List<Long> userIdList = membersVOList.stream().map(TechTeamMembersVO::getUserId).collect(Collectors.toList());
            List<TechTaskSubmissionsVO> techTaskSubmissionsVOList = taskSubmissionsMapper.getByUserIdAndTaskId(taskIdList, userIdList);
            if (CollectionUtil.isNotEmpty(techTaskSubmissionsVOList)) {
                List<Long> submissionIds = techTaskSubmissionsVOList.stream().map(TechTaskSubmissionsVO::getId).collect(Collectors.toList());
                // 删除团队成员正在执行的任务
                taskSubmissionsMapper.deleteById(submissionIds);
                // 删除对应的资源
                techTaskSubmissionsVOList.stream().forEach(submission -> {
                    if (submission.getRelatedType().equals(RelatedTypeEnum.POST.name().toLowerCase())) {
                        TechPostVO techPostVO = postMapper.getById(submission.getRelatedId());
                        minioTemplate.removeFile(techPostVO.getCode());
                        // 删除项目
                        postMapper.delete(techPostVO.getId());
                    } else {
                        TechCollectionVO techCollectionVO = collectionMapper.getById(submission.getRelatedId());
                        // 删除合集
                        collectionMapper.delete(techCollectionVO.getId());
                        // 删除合集下的项目
                        List<TechPostVO> techPostVOList = postMapper.getPostByCollectionId(techCollectionVO.getId());
                        techPostVOList.stream().forEach(techPostVO -> {
                            minioTemplate.removeFile(techPostVO.getCode());
                            // 删除项目
                            postMapper.delete(techPostVO.getId());
                        });
                    }
                });
            }
        }
        // 删除团队成员
        teamMemberMapper.batchDeleteMembers(memberIds);
    }

    @Override
    public void deleteMembers(Long id) {
        TechTeamMembersVO techTeamMembersVO =  teamMemberMapper.getOneById(id);
        teamMemberMapper.deleteMembers(id);
        List<TechTaskVO> taskVOList = taskMapper.getTasksByTeamId(techTeamMembersVO.getTeamId());
        if (CollectionUtil.isNotEmpty(taskVOList)) {
            List<Long> longList = taskVOList.stream().map(TechTaskVO::getId).collect(Collectors.toList());
            // 删除任务
            taskSubmissionsMapper.deleteByUserId(techTeamMembersVO.getUserId(),longList);
        }

    }
}
